﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using HeartLess.GameObjects;

namespace HeartLess.Core
{
    public static class Screen
    {

        private static Vector2 _cameraOffset;
        
        public const int WINDOW_WIDTH = 800;
        public const int WINDOW_HEIGHT = 480;
        public static readonly Vector2 WINDOW_CENTER = new Vector2(WINDOW_WIDTH/2, WINDOW_HEIGHT/2);
        public static readonly Vector2 HALF_TILE = new Vector2(50, 50);

        public static readonly Rectangle WINDOW_BOUNDS = new Rectangle(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT);

        public static Rectangle CameraBounds
        {
            get;
            private set;
        }

        public static Vector2 CameraLocation
        {
            get;
            private set;
        }

        static Screen()
        {
            _cameraOffset = Vector2.Zero;
            CameraBounds = new Rectangle(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT);
        }

        public static void FocusCameraOn(Vector2 targetLocation)
        {
            _cameraOffset = WINDOW_CENTER - targetLocation - HALF_TILE;
            if (_cameraOffset.Y < 0) _cameraOffset.Y = 0;

            CameraLocation = -_cameraOffset;

            CameraBounds = new Rectangle(-(int)_cameraOffset.X,
                                          -(int)_cameraOffset.Y,
                                          WINDOW_WIDTH,
                                          WINDOW_HEIGHT);
        }

        public static void Draw(SpriteBatch spriteBatch, Vector2 location, Texture2D texture)
        {
            Draw(spriteBatch, location, texture, null, SpriteEffects.None);
        }

        public static void Draw(SpriteBatch spriteBatch, Rectangle destination, Texture2D texture)
        {
            destination.Offset((int)_cameraOffset.X, (int)_cameraOffset.Y);
            spriteBatch.Draw(texture, destination, null, Color.White);
        }

        public static void Draw(SpriteBatch spriteBatch, Vector2 location, Texture2D texture, Rectangle? sourceRectangle)
        {
            Draw(spriteBatch, location, texture, sourceRectangle, SpriteEffects.None);
        }

        public static void Draw(SpriteBatch spriteBatch, Vector2 location, Texture2D texture, Rectangle? sourceRectangle, SpriteEffects spriteEffects)
        {
            spriteBatch.Draw(texture, location + _cameraOffset, sourceRectangle, Color.White, 0, Vector2.Zero, 1f, spriteEffects, 0f);
        }

        public static void Draw(SpriteBatch spriteBatch, Vector2 location, Texture2D texture, Rectangle? sourceRectangle, Color tint, SpriteEffects spriteEffects)
        {
            spriteBatch.Draw(texture, location + _cameraOffset, sourceRectangle, tint, 0, Vector2.Zero, 1f, spriteEffects, 0f);
        }
    }
}
